﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AgendaDoEstudante.Models;

namespace Agenda_do_Estudante.Controllers
{
    public class AgendaController : Controller
    {
        private AgendaContext db = new AgendaContext();
        private UsersContext dbUsers = new UsersContext();

        //
        // GET: /Agenda/

        public ActionResult Index()
        {
            int userId = (dbUsers.UserProfiles.Single(u => u.UserName == User.Identity.Name)).UserId;
            List<Agenda> agendas = db.Agendas.Where(l => l.UserId == userId).ToList();
            return View(agendas);
        }

        //
        // GET: /Agenda/Details/5

        public ActionResult Details(int id = 0)
        {
            Agenda agenda = db.Agendas.Find(id);
            if (agenda == null)
            {
                return HttpNotFound();
            }
            return View(agenda);
        }

        //
        // GET: /Agenda/Create

        public ActionResult Create()
        {
            var agenda = new Agenda() { UserId = (dbUsers.UserProfiles.Single(u => u.UserName == User.Identity.Name)).UserId, Data = DateTime.Now };
            return View(agenda);
        }

        //
        // POST: /Agenda/Create

        [HttpPost]
        public ActionResult Create(Agenda agenda)
        {
            if (ModelState.IsValid)
            {
                db.Agendas.Add(agenda);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(agenda);
        }

        //
        // GET: /Agenda/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Agenda agenda = db.Agendas.Find(id);
            if (agenda == null)
            {
                return HttpNotFound();
            }
            return View(agenda);
        }

        //
        // POST: /Agenda/Edit/5

        [HttpPost]
        public ActionResult Edit(Agenda agenda)
        {
            if (ModelState.IsValid)
            {
                db.Entry(agenda).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(agenda);
        }

        //
        // GET: /Agenda/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Agenda agenda = db.Agendas.Find(id);
            if (agenda == null)
            {
                return HttpNotFound();
            }
            return View(agenda);
        }

        //
        // POST: /Agenda/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Agenda agenda = db.Agendas.Find(id);
            db.Agendas.Remove(agenda);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}